package CustomUI;

import org.Consts;
import org.sqlStatements;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.dworld3.Draw;
import com.ydev.dworld3.R;

import DTO.Properties;
import android.R.bool;
import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class ShapePropertiesDialog extends Dialog {

	SeekBar skWidth = new SeekBar(getContext());
	TextView tvWidth = new TextView(getContext());
	CheckBox chFill, chSym;

	Properties props = new Properties(sqlStatements.DC_SHAPE);

	public ShapePropertiesDialog(final Context context) {
		super(context);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.shape_properties_dialog);
		this.getWindow().setBackgroundDrawable(
				new ColorDrawable(android.graphics.Color.TRANSPARENT));
		LayoutParams params = getWindow().getAttributes();
		params.width = LayoutParams.FILL_PARENT;
		getWindow().setAttributes(
				(android.view.WindowManager.LayoutParams) params);

		tvWidth = (TextView) findViewById(R.id.tVWidth);

		chFill = (CheckBox) findViewById(R.id.checkBoxFill);
		chSym = (CheckBox) findViewById(R.id.checkBoxSym);

		skWidth = (SeekBar) findViewById(R.id.skWidth);
		
		ydButton btnOK=(ydButton)findViewById(R.id.ydButtonOK);
	        btnOK.addOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
				//	((Draw)context).setShapePathProperties(getShape(), getBrushWidth(), getFillProperty(), getSymetric());
					((Draw)context).setShapePathProperties(props);
					hide();
					
				}
			});

		final ydButton ydButtonShape = (ydButton) findViewById(R.id.ydButtonShape);
		ydButtonShape.setSelected(true);

		final ydButton ydButtonOtherShape1 = (ydButton) findViewById(R.id.ydButtonOtherShape1);
		// changeShapeClicked(ydButtonShape, ydButtonOtherShapes);

		final ydButton ydButtonOtherShape2 = (ydButton) findViewById(R.id.ydButtonOtherShape2);
		// changeShapeClicked(ydButtonShape, ydButtonOtherShapes);

		final ydButton ydButtonOtherShape3 = (ydButton) findViewById(R.id.ydButtonOtherShape3);

		ydButtonShape.addOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ydButtonShape.setSelected(true);
				ydButtonOtherShape1.setSelected(false);
				ydButtonOtherShape2.setSelected(false);
				ydButtonOtherShape3.setSelected(false);
				props.shape = sqlStatements.DC_RECTANGLE;
			}
		});

		ydButtonOtherShape1.addOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ydButtonShape.setSelected(false);
				ydButtonOtherShape1.setSelected(true);
				ydButtonOtherShape2.setSelected(false);
				ydButtonOtherShape3.setSelected(false);
				props.shape = sqlStatements.DC_ARC;
			}
		});

		ydButtonOtherShape2.addOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ydButtonShape.setSelected(false);
				ydButtonOtherShape1.setSelected(false);
				ydButtonOtherShape2.setSelected(true);
				ydButtonOtherShape3.setSelected(false);
				props.shape = sqlStatements.DC_LINE;
			}
		});

		ydButtonOtherShape3.addOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				ydButtonShape.setSelected(false);
				ydButtonOtherShape1.setSelected(false);
				ydButtonOtherShape2.setSelected(false);
				ydButtonOtherShape3.setSelected(true);
				props.shape = sqlStatements.DC_CIRCLE;
			}
		});

		skWidth.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				tvWidth.setText((skWidth.getProgress() + 1) + "");
				props.setSize(progress+1);
			}
		});
	
		chFill.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
					props.fill=1;
				else
					props.fill=0;
				
			}
		});
		
		chSym.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked)
					props.sym=1;
				else
					props.sym=0;
				
			}
		});
	}

	protected String getShape() {
		// TODO Auto-generated method stub
		return props.shape;
	}

	protected boolean getSymetric() {
		// TODO Auto-generated method stub
		return chSym.isChecked();
	}

	public int getBrushWidth() {

		return skWidth.getProgress() + 1;
	}

	public boolean getFillProperty() {
		return chFill.isChecked();
	}

	public void changeShapeClicked(final ydButton current, final ydButton chosen) {
		chosen.addOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {

				chosen.setSelected(true);
			}
		});
	}

}
